﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess.SqlServer
{
    public class SqlServerBacLuong: IBacLuong
    {

        public BacLuong GetBacLuong(int oid)
        {
            string sql = @" Select NgachLuong, MaQuanLy, TenBacLuong, HeSoLuong
                            From BacLuong
                            Where Oid = @oid";
            SqlParameter param = new SqlParameter("@oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            BacLuong bacLuong = new BacLuong();

            bacLuong.Oid = oid;
            int ngachLuong;
            if (int.TryParse(row["NgachLuong"].ToString(), out ngachLuong))
            {
                SqlServerNgachLuong sqlServerNgachLuong = new SqlServerNgachLuong();
                bacLuong.NgachLuong = sqlServerNgachLuong.GetNgachLuong(ngachLuong);
            }
            bacLuong.MaQuanLy = row["MaQuanLy"].ToString();
            bacLuong.TenBacLuong = row["TenBacLuong"].ToString();
            bacLuong.HeSoLuong = decimal.Parse(row["HeSoLuong"].ToString());

            return bacLuong;
        }

        public List<BacLuong> GetListBacLuong(NgachLuong ngachLuong)
        {
            string sql = @" Select Oid, MaQuanLy, TenBacLuong, HeSoLuong
                            From BacLuong
                            Where NgachLuong = @NgachLuong";

            SqlParameter param = new SqlParameter("@NgachLuong", ngachLuong.Oid);
            DataTable dt = DataProvider.GetDataTable(sql,param);
            List<BacLuong> list = new List<BacLuong>();

            foreach (DataRow row in dt.Rows)
            {
                BacLuong bacLuong = new BacLuong();

                bacLuong.Oid = int.Parse(row["Oid"].ToString());
                bacLuong.NgachLuong = ngachLuong;
                bacLuong.MaQuanLy = row["MaQuanLy"].ToString();
                bacLuong.TenBacLuong = row["TenBacLuong"].ToString();
                bacLuong.HeSoLuong = decimal.Parse(row["HeSoLuong"].ToString());

                list.Add(bacLuong);
            }
            return list;
        }

        public List<BacLuong> GetListBacLuong()
        {
            string sql = @" Select Oid, MaQuanLy, TenBacLuong, HeSoLuong, NgachLuong
                            From BacLuong";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<BacLuong> list = new List<BacLuong>();

            foreach (DataRow row in dt.Rows)
            {
                BacLuong bacLuong = new BacLuong();

                bacLuong.Oid = int.Parse(row["Oid"].ToString());
                int ngachLuong;
                if (int.TryParse(row["NgachLuong"].ToString(), out ngachLuong))
                {
                    SqlServerNgachLuong sqlServerNgachLuong = new SqlServerNgachLuong();
                    bacLuong.NgachLuong = sqlServerNgachLuong.GetNgachLuong(ngachLuong);
                }
                bacLuong.MaQuanLy = row["MaQuanLy"].ToString();
                bacLuong.TenBacLuong = row["TenBacLuong"].ToString();
                bacLuong.HeSoLuong = decimal.Parse(row["HeSoLuong"].ToString());

                list.Add(bacLuong);
            }
            return list;
        }
        public int Insert(BacLuong bacLuong)
        {
            string sql = @" Insert into BacLuong(NgachLuong, MaQuanLy, TenBacLuong, HeSoLuong)
                            Values(@NgachLuong, @MaQuanLy, @TenBacLuong, @HeSoLuong)";

            SqlParameter[] param = {
                                       new SqlParameter("@NgachLuong",bacLuong.NgachLuong.Oid),
                                       new SqlParameter("@MaQuanLy", bacLuong.MaQuanLy),
                                       new SqlParameter("@TenBacLuong", bacLuong.TenBacLuong),
                                       new SqlParameter("@HeSoLuong", bacLuong.HeSoLuong)
                                   };
            int id = DataProvider.Insert(sql, true, param);
            if (id > 0)
                bacLuong.Oid = id;
            return bacLuong.Oid;
        }

        public int Update(BacLuong bacLuong)
        {
            string sql = @" Update BacLuong
                            Set NgachLuong = @NgachLuong,
                                MaQuanLy = @MaQuanLy,
                                TenBacLuong = @TenBacLuong,
                                HeSoLuong = @HeSoLuong
                            Where Oid = @Oid";

            SqlParameter[] param = {
                                       new SqlParameter("@Oid",bacLuong.Oid),
                                       new SqlParameter("@NgachLuong",bacLuong.NgachLuong.Oid),
                                       new SqlParameter("@MaQuanLy", bacLuong.MaQuanLy),
                                       new SqlParameter("@TenBacLuong", bacLuong.TenBacLuong),
                                       new SqlParameter("@HeSoLuong", bacLuong.HeSoLuong)
                                   };
            return DataProvider.Update(sql, param);
            
        }

        public int Delete(BacLuong bacLuong)
        {
            string sql = @" Delete from BacLuong Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", bacLuong.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(BacLuong obj)
        {
            throw new NotImplementedException();
        }
    }
}
